package com.fly.uaa.security;

import com.fly.common.model.UserEntity;
import com.fly.common.util.StringUtil;
import com.fly.uaa.model.CustomerUserDetail;
import com.fly.uaa.service.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 用户登录业务
 * @author: fly
 * @date: 2018/11/30 17:48
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private LoginService loginService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 如果是admin用户注解赋予ROLE_SERVER权限
        if ("admin".equals(username)) {
            List<GrantedAuthority> grantedAuthorities = getGrantedAuthorities("ROLE_SERVER");
            // 加密前密码：E8zvZXDqzB1QT1pO 加密后：77e4371b2f5ba909495da32f983cceca
            return new CustomerUserDetail(0L, username, "77e4371b2f5ba909495da32f983cceca", grantedAuthorities);
        }

        // 根据账户获取用户信息
        UserEntity user = loginService.queryUserByUserName(username);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        }

        // 用户角色权限
        List<GrantedAuthority> grantedAuthorities = getGrantedAuthorities("ROLE_USER");
        return new CustomerUserDetail(user.getUserId(), user.getMobile(), StringUtils.defaultString(user.getPassword()), grantedAuthorities);
    }

    /**
     * 获取权限点
     *
     * @param role 权限名称
     * @return 权限点集合
     */
    private List<GrantedAuthority> getGrantedAuthorities(String role) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);
        grantedAuthorities.add(authority);
        return grantedAuthorities;
    }

    /**
     * 根据主键获取用户信息
     *
     * @param userId 用户主键
     * @return 用户信息
     * @throws UsernameNotFoundException 未发现用户异常
     */
    public UserDetails loadUserByUserId(long userId) throws UsernameNotFoundException {
        UserEntity user = loginService.queryUserById(userId);
        if (user == null) {
            throw new UsernameNotFoundException("userId: " + userId);
        }

        // 用户角色权限
        List<GrantedAuthority> grantedAuthorities = getGrantedAuthorities("ROLE_USER");
        return new CustomerUserDetail(user.getUserId(), user.getMobile(), StringUtils.defaultString(user.getPassword()), grantedAuthorities);
    }

    public boolean isSmsCodeValid(String username, String code){
        String smsCode = "fuliying";
        if (StringUtil.isEmpty(smsCode)) {
            return false;
        }

        // 对比当前有效的验证码和用户输入的验证码
        if (code.equals(smsCode)) {
            return true;
        } else{
            return false;
        }
    }
}